/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.database.pojo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import org.w3c.dom.Element;

/**
 * @author fred
 */
@Entity
public class VinnyOption implements DatabasePojo {

	/**
	 * Auto authorize non-issue services?
	 */
	public static final String AUTO_AUTHORIZE = "AutoAuthorize";
	/**
	 * Default service time, when not using operating hours.
	 */
	public static final String DEFAULT_SERVICE_TIME = "DefaultServiceTime";

	/**
	 * Display Service Types
	 */
	public static final String DISPLAY_SERVICE_TYPES = "DisplayServiceTypes";

	/**
	 * The first month of the year for reporting purposes.
	 */
	public static final String FIRST_MONTH_OF_YEAR = "FirstMonthOfYear";

	/**
	 * Product icon file name
	 */
	public static final String ICON_FILE_NAME = "IconFileName";

	/**
	 * Minimum time between scans before they are considered for a service.
	 */
	public static final String MINIMUM_SERVICE_TIME = "MinimumServiceTime";

	/**
	 * Name of the product.
	 */
	public static final String PRODUCT_NAME = "ProductName";

	/**
	 * Sponsoring organization
	 */
	public static final String SERVICE_OF = "ServiceOf";

	/**
	 * Use operating hours when calculating service times.
	 */
	public static final String USE_OPERATING_HOURS = "UseOperatingHours";

	private boolean active;
	private int id;
	private Location location;
	private String parameter;
	private String value;

	public VinnyOption() {
		// empty
	}

	public VinnyOption(final String parameter, final String value, final Location location) {
		this.active = true;
		this.parameter = parameter;
		this.location = location;
		this.value = value;
	}

	@Override
	public void attachAttributes(final Element element) {
		element.setAttribute("id", Integer.toString(this.getId()));
		element.setAttribute("active", Boolean.toString(this.isActive()));
		element.setAttribute("location", Integer.toString(this.getLocation().getId()));
		element.setAttribute("parameter", this.getParameter());
		element.setAttribute("value", this.getValue());
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof VinnyOption)) {
			return false;
		}
		final VinnyOption other = (VinnyOption) obj;
		if (this.getLocation() == null) {
			if (other.getLocation() != null) {
				return false;
			}
		} else if (!this.getLocation().equals(other.getLocation())) {
			return false;
		}
		if (this.getParameter() == null) {
			if (other.getParameter() != null) {
				return false;
			}
		} else if (!this.getParameter().equals(other.getParameter())) {
			return false;
		}
		if (this.getValue() == null) {
			if (other.getValue() != null) {
				return false;
			}
		} else if (!this.getValue().equals(other.getValue())) {
			return false;
		}
		return true;
	}

	@Override
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return this.id;
	}

	/**
	 * @return the location
	 */
	@ManyToOne
	public Location getLocation() {
		return this.location;
	}

	/**
	 * @return the parameter
	 */
	public String getParameter() {
		return this.parameter;
	}

	/**
	 * @return the value
	 */
	public String getValue() {
		return this.value;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (prime * result) + (this.getLocation() == null ? 0 : this.getLocation().hashCode());
		result = (prime * result) + (this.getParameter() == null ? 0 : this.getParameter().hashCode());
		result = (prime * result) + (this.getValue() == null ? 0 : this.getValue().hashCode());
		return result;
	}

	@Override
	public boolean isActive() {
		return this.active;
	}

	/**
	 * @param active
	 *            the active to set
	 */
	public void setActive(final boolean active) {
		this.active = active;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(final int id) {
		this.id = id;
	}

	/**
	 * @param location
	 *            the location to set
	 */
	@ManyToOne
	public void setLocation(final Location location) {
		this.location = location;
	}

	/**
	 * @param parameter
	 *            the parameter to set
	 */
	public void setParameter(final String parameter) {
		this.parameter = parameter;
	}

	/**
	 * @param value
	 *            the value to set
	 */
	public void setValue(final String value) {
		this.value = value;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Option [id=" + this.getId() + ", location=" + this.getLocation() + ", parameter=" + this.getParameter() + ", value=" + this.getValue() + "]";
	}
}
